package org.grayrabbit.cms.publish.generate.factory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.entity.Topic;
import org.grayrabbit.cms.publish.api.Generatable;
import org.grayrabbit.cms.publish.generate.Generater;
import org.grayrabbit.cms.publish.generate.MultiGenerater;
import org.grayrabbit.cms.publish.generate.TopicListGenerater;

import freemarker.template.Configuration;

/**
 * @author Administrator
 * @version 1.0
 * @created 15-����-2011 10:43:13
 */
public class TopicListGeneraterFactory implements GeneraterFactory {
	private Configuration configuration;
	private List<Topic> topics = new ArrayList<Topic>();
	private String rootPath;
	private int pageSize;
	private int pageNum;
	private Map<String,List<Content>> contents;
	private Generatable generatable;
	private Admin admin;
	public TopicListGeneraterFactory(Configuration configuration, List<Topic> topics, String rootPath,Map<String,List<Content>> contents,int pageSize,int pageNum,Generatable generatable,Admin admin){
		this.configuration = configuration;
		this.topics = topics;
		this.rootPath = rootPath;
		this.contents = contents;
		this.pageSize = pageSize;
		this.pageNum = pageNum;
		this.generatable = generatable;
		this.admin = admin;
	}
	
	public TopicListGeneraterFactory(Configuration configuration, Topic topic, String rootPath,Generatable generatable,Admin admin){
		this.configuration = configuration;
		this.topics.add(topic);
		this.rootPath = rootPath;
		this.generatable = generatable;
		this.admin = admin;
	}


	public Generater create(){
		List<Generater> list = new ArrayList<Generater>();
		for (Topic topic : topics) {
			String path = topic.getTopicContextListTpl().substring((File.separator+rootPath).length());
			if(contents.get(topic.getTopicId())!=null){
				List<Content> cList = new ArrayList<Content>();
				for(Content obj : contents.get(topic.getTopicId())){
					obj.setTopic(topic);
					cList.add(obj);
				}
				int maxPage = (contents.get(topic.getTopicId()).size()%pageSize==0)?(contents.get(topic.getTopicId()).size()/pageSize):(contents.get(topic.getTopicId()).size()/pageSize+1);
				if(maxPage>1){
					for(int i = 0;i<maxPage;i++){
						List<Content> rs = getSubList(cList,i+1);
						if(rs!=null){
							list.add(new TopicListGenerater(configuration,path,topic,null,i+1,maxPage,rs,generatable,admin));
						}
					}
				}else{
					list.add(new TopicListGenerater(configuration,path,topic,null,0,maxPage,cList,generatable,admin));
				}
			}else{
				list.add(new TopicListGenerater(configuration,path,topic,null,pageNum,pageNum,null,generatable,admin));
			}
		}
		if(list.size()==1)
			return list.get(0);
		return new MultiGenerater(list);
	}
	
	private List<Content> getSubList(List<Content> list,int page){
		int size = list.size();
		if(size<=10){
			return list;
		}
		else if((page*10)>(size+10)){
			return null;
		}
		else{
			int i =page*10>size?size:page*10;
			List<Content> obj = new ArrayList<Content>(list.subList((page-1)*10, i));
			return obj;
		}
	}

}